package com.rules.admin.controller.decision;


import com.google.common.collect.Maps;
import com.rules.admin.entity.DecisionCell;
import com.rules.admin.entity.DecisionRow;
import com.rules.admin.entity.DecisionTable;
import com.rules.admin.service.decision.DecisionRowService;
import com.rules.admin.service.decision.DecisionTableService;
import com.rules.admin.utils.ResultUtils;
import com.rules.admin.vo.DecisionColConfig;
import com.rules.admin.vo.DecisionColVO;
import com.rules.admin.vo.DecisionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping({"/decision-tables"})
public class DecisionTableController {
   @Autowired
   private DecisionTableService decisionTableService;
   @Autowired
   private DecisionRowService decisionRowService;

   @RequestMapping(
      method = {RequestMethod.POST}
   )
   @ResponseBody
   public Map<String, Object> createDecisionTable(@RequestBody DecisionTable decisionTable) {
      this.decisionTableService.create(decisionTable);
      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      value = {"/{id}"},
      method = {RequestMethod.DELETE}
   )
   @ResponseBody
   public Map<String, Object> deleteDecisionTable(@PathVariable Long id) {
      this.decisionTableService.deleteDecision(id);
      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      value = {"/{id}"},
      method = {RequestMethod.PUT}
   )
   @ResponseBody
   public Map<String, Object> updateDecisionTable(@RequestBody DecisionTable decisionTable) {
      this.decisionTableService.updateById(decisionTable);
      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      value = {"/{id}"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public Map<String, Object> getDecisionTables(@PathVariable long id) {
      DecisionTable decisionTable = (DecisionTable)this.decisionTableService.getById(id);
      return ResultUtils.getSuccessResultData(decisionTable);
   }

   @RequestMapping(
      value = {"/getCols/{tableId}"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public Map<String, Object> getColAttributes(@PathVariable long tableId) {
      return ResultUtils.getSuccessResultData(this.decisionTableService.getCols(tableId));
   }

   @RequestMapping(
      value = {"/saveColConfig/{tableId}"},
      method = {RequestMethod.POST}
   )
   @ResponseBody
   public Map<String, Object> saveColConfig(@PathVariable long tableId, @RequestBody DecisionColConfig decisionColConfig) {
      this.decisionTableService.saveColConfig(tableId, decisionColConfig);
      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      value = {"/loadDecision"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public Map<String, Object> loadDecision(@RequestParam long tableId) {
      List<DecisionColVO> cols = this.decisionTableService.getCols(tableId);
      List<DecisionRow> rows = this.decisionRowService.getRowsByTableId(tableId);
      List<DecisionCell> cells = this.decisionTableService.getCells(tableId);
      Map<String, Object> data = Maps.newHashMap();
      data.put("cols", cols);
      data.put("rows", rows);
      data.put("cells", cells);
      return ResultUtils.getSuccessResultData(data);
   }

   @RequestMapping(
      value = {"/saveDecision"},
      method = {RequestMethod.POST}
   )
   @ResponseBody
   public Map<String, Object> saveDecision(@RequestBody DecisionVO decisionVO) {
      this.decisionTableService.saveDecision(decisionVO);
      return ResultUtils.getSuccessResultData();
   }
}
